﻿<%@ WebHandler Language="C#" Class="danweihuizong" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using ServiceStack.Redis;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class danweihuizong : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
           string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string zhanDian = context.Request.QueryString["zhandian"];
            string chengZhongLeiXing = context.Request.QueryString["chengzhongleixing"];
            string kaiShiShiJian = context.Request.QueryString["kaishishijian"];
            string jieShuShiJian = context.Request.QueryString["jieshushijian"];
            string wuLiaoMingCheng = context.Request.QueryString["wuliaomingcheng"];
            string xingHao = context.Request.QueryString["xinghao"];
            if (string.IsNullOrEmpty(chengZhongLeiXing) || string.IsNullOrEmpty(zhanDian))
            {
                context.Response.Write("[]");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                string createCondition = m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                {
                    if (chengZhongLeiXing == "入库物料")
                    {
                        createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    }
                    else
                    {
                        createCondition += m_DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    }
                }

                if (!string.IsNullOrEmpty(wuLiaoMingCheng) && wuLiaoMingCheng != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(xingHao) && wuLiaoMingCheng != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xingHao, "", SearchConditionType.等于);
                }
                string tableCols = string.Empty;
                string groupCols = string.Empty;
                Dictionary<string, string> orderBy = new Dictionary<string, string>();
                orderBy.Add("wuliaomingcheng", "asc");
                orderBy.Add("xinghao", "asc");
                tableCols = "danweimingcheng,wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                groupCols = "danweimingcheng,wuliaomingcheng,xinghao";
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);
                tableCols = "wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                groupCols = "wuliaomingcheng,xinghao";
                DataTable DtCount = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);

                if (Dt.Rows.Count > 0)
                {
                    for (int i = 0; i < Dt.Rows.Count; i++)
                    {
                        if (i == Dt.Rows.Count - 1)
                        {
                            DataRow newRow = Dt.NewRow();
                            newRow["xinghao"] = "合计：";
                            newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                            newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                            newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                            newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                            newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                            newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                            newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                            Dt.Rows.InsertAt(newRow, i + 1);
                            DtCount.Rows.RemoveAt(0);
                            i++;
                            break;
                        }
                        if (!Dt.Rows[i]["wuliaomingcheng"].Equals(Dt.Rows[i + 1]["wuliaomingcheng"]) || !Dt.Rows[i]["xinghao"].Equals(Dt.Rows[i + 1]["xinghao"]))
                        {
                            DataRow newRow = Dt.NewRow();
                            newRow["xinghao"] = "合计：";
                            newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                            newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                            newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                            newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                            newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                            newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                            newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                            Dt.Rows.InsertAt(newRow, i + 1);
                            DtCount.Rows.RemoveAt(0);
                            i++;
                        }
                    }
                    string jsonStr = JsonConvert.SerializeObject(Dt);
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null)
                        {
                            redis.SetValue(key, jsonStr, TimeSpan.FromMinutes(10.0));
                        }
                    }
                    context.Response.Write(jsonStr);
                }
                else
                {
                    context.Response.Write("[]");
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("查询单位汇总信息异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}